export default {
  data() {
    return {
      rename: false,
      curFile: {},
      ext: '',
    }
  },
  computed: {
    newName: {
      get() {
        let { oldName, isFolder } = this.curFile
        if (!oldName) return
        if (isFolder == 1) {
          return oldName
        } else {
          let idx = oldName.lastIndexOf('.')
          this.ext = oldName.slice(idx)
          return oldName.slice(0, idx)
        }
      },
      set(v) {
        this.curFile.oldName = v + this.ext
        return v
      },
    },
  },
  mounted() {
    this.$bus.$on('showRenameInput', (row) => {
      this.curFile = JSON.parse(JSON.stringify(row))
      this.rename = true
      setTimeout(() => {
        this.$refs['ipt' + this.curFile.id][0].focus()
        // this.$refs['ipt' + this.curFile.id][0].select()
      })
    })
  },
  beforeDestroy() {
    this.$bus.$off('showRenameInput')
  },
  methods: {
    async handleSaveName() {
      let { id, oldName } = this.curFile
      await this.$store.dispatch('updateTbNetDisk', { id, oldName })
      this.$bus.$emit('refreshTbNetDiskList')
      this.handleCloseName()
    },
    handleCloseName() {
      this.curFile = {}
      this.rename = false
    },
  },
}
